var addressInfoPage;
// 初始化分页对象
var pageSize;
var status;
$(function() {
	$("#creatNewAddress").hide();
	$("#guideValidationDIV").hide();

	getAllProvince();
	addressInfoPage = new pagerClass();
	pageSize = $('#pageSize').val();
	status = $('#status').val();
	addressInfoPage.init(1, 1, pageSize, 0, "detailPagination");
	Switching(3);

});

// 切换选项卡，改变他的样式
var currTab = "";
function Switching(value) {
	$("#creatNewAddress").hide();
	$("#guideValidationDIV").hide();
	$('#status').val(value);
	if (currTab == value) {
		return;
	}

	currTab = value;
	if (value == 3) {
		$("#allAddressInfoTab").addClass("on");
		$("#verifyAddressInfoTab").removeClass("on");
		$("#noVerifyAddressInfoTab").removeClass("on");

		getAllAddressInfoData(1, 3);
	} else if (value == 2) {
		$("#verifyAddressInfoTab").addClass("on");
		$("#allAddressInfoTab").removeClass("on");
		$("#noVerifyAddressInfoTab").removeClass("on");
		getAllAddressInfoData(1, 2);
	} else if (value == 1) {
		$("#allAddressInfoTab").removeClass("on");
		$("#verifyAddressInfoTab").removeClass("on");
		$("#noVerifyAddressInfoTab").addClass("on");
		getAllAddressInfoData(1, 1);

	}
};


// 获取所有的地址信息
function getAllAddressInfoData(pageIndex, status) {
	window.scrollTo(0, 0);
	addressInfoPage.clear();
	cancleCreate();
	addressInfoPage.index = pageIndex;
	status = $('#status').val();
	$.ajax({
        url : "../tree/findAddress",
        data : {
            pageIndex : pageIndex,
            status : status,
            source:"pc"
        },
        type : "Post",
        dataType : "json",
        success : function(page) {
            if (page == null || page.count == 0) {
                $("#adressInfos").html("<div class=\"no-getting-infos\">暂无记录</div>");
            } else {
                var newData = page.data;
                addressInfoPage.setRows(page.count);
                //判断是否有失效地址
                var strHtml = "<ul>";
                $.each(newData,function(i, addressItem) {
                    var invalidAddress = !addressItem.match;
                    // 首选未验证
                    if (addressItem.preferredStatus == 2) {
                        strHtml += "<li class='m_c2'>	";
                        strHtml += "<h2 class='f13 c1 mt10 ml10'><p class='fr'><a href='#' onclick='modifyAddress("
                                + addressItem.addressId
                                + ");' class='fl mr10 m_c3'>修改</a><span class='m_c0 fl mr10'>|</span><a href='#' class='fl mr10 m_c3' onclick='deleteAddressById("
                                + addressItem.addressId
                                + ");'>删除</a>";
                        if (invalidAddress) {
                            strHtml += "<br><span style='background-color:rgba(254,44,120,1);color:rgba(255,255,255,1);float:right;margin-right:10px;'>地址已失效</span>";
                        }
                        strHtml += "</p></h2>";
                        strHtml += "<dl class='m_add_dl'>";
                        strHtml += "<dd>"
                                + addressItem.recipientName
                                + " "
                                + addressItem.mobile
                                + " "
                                + addressItem.telephone
                                + "</dd>";
                        strHtml += "<dd style='width:580px;word-break:break-all;'>"
                                + addressItem.distributionProvince
                                + " "
                                + addressItem.distributionCity
                                + " "
                                + addressItem.distributionCounty
                                + " "
                                + addressItem.distributionStreet
                                + " "
                                + addressItem.distributionDeatilInfo
                                + "</dd>";
                        strHtml += "</dl>";
                        strHtml += "<a href='#' class='m_close_two'><strong>"
                                + "首选地址"
                                + "</strong></a>";
                        if (invalidAddress) {
                            strHtml += "<p class='m_add_p mt10 pt10'><span class='c4'>"+page.invalidAddressText+"</span></p>";
                        }
                        // 修改DIV
                        strHtml += "	<div class='content per-edit-list per-wd02' id='modify"
                                + addressItem.addressId
                                + "' style='display:none;'>";
                        strHtml += getModifyString(
                                addressItem.addressId,
                                invalidAddress);
                        strHtml += "		</div>";
                    } else{
                        strHtml += "<li class='m_c2'>	";
                        strHtml += "<h2 class='f13 c1 mt10 ml10'><p class='fr'><a href='#'  onclick='addressAsPreferred("
                                + addressItem.addressId+ ",\""
                                + addressItem.addressName
                                + "\");' class='fl mr10 m_c3'>设为首选</a><span class='m_c0 fl mr10'>|</span> <a  onclick='modifyAddress("
                                + addressItem.addressId
                                + ");' href='#' class='fl mr10 m_c3'>修改</a><span class='m_c0 fl mr10'>|</span> <a href='#' class='fl mr10 m_c3' onclick='deleteAddressById("
                                + addressItem.addressId
                                + ");'>删除</a>";
                        if (invalidAddress) {
                            strHtml += "<br><span style='background-color:rgba(254,44,120,1);color:rgba(255,255,255,1);float:right;margin-right:10px;'>地址已失效</span>";
                        }
                        strHtml += "</p></h2>";
                        strHtml += "<dl class='m_add_dl'>";
                        strHtml += "<dd>"
                                + addressItem.recipientName
                                + " "
                                + addressItem.mobile
                                + " "
                                + addressItem.telephone
                                + "</dd>";
                        strHtml += "<dd style='width:580px;word-break:break-all;'>"
                                + addressItem.distributionProvince
                                + " "
                                + addressItem.distributionCity
                                + " "
                                + addressItem.distributionCounty
                                + " "
                                + addressItem.distributionStreet
                                + " "
                                + addressItem.distributionDeatilInfo
                                + "</dd>";
                        strHtml += "</dl>";
                        strHtml += "<a href='#' class='m_close_two'><strong></strong></a>";
                        if (invalidAddress) {
                            strHtml += "<p class='m_add_p mt10 pt10'><span class='c4'>"+page.invalidAddressText+"</span></p>";
                        }
                        // 修改
                        strHtml += "	<div class='content per-edit-list per-wd02' id='modify"
                                + addressItem.addressId
                                + "' style='display:none;'>";

                        strHtml += getModifyString(
                                addressItem.addressId,
                                invalidAddress);
                        strHtml += "		</div>";

                    }
                    strHtml += "  </li>";

                });
                $("#adressInfos").html(strHtml + "</ul>");
                addressInfoPage.process("getAllAddressInfoData",
                        "detailPagination");
            }
        }
    });
};

function cancleCreate() {
	$("#creatNewAddress").hide();
};

function creatnewAddress() {
    $("#creatNewAddress").show();
};

function getAllProvince() {
	$.ajax({
		url : "getAllProvince",
		type : "Post",
		dataType : "html",
		success : function(data) {
			var Province = eval(data);
			var tempList = $("#ProvinceSelect");
			$("#ProvinceSelect option").remove();
			var countryList = $("#countrySelect");
			$("#countrySelect option").remove();
			countryList.append("<option value='-1'>--请选择--</option>");
			tempList.append("<option value='-1'>--请选择--</option>");
			$(Province).each(
					function(key) {
						var opt = $("<option></option>").text(
								Province[key].areaName).val(
								Province[key].areaId);
						tempList.append(opt);
					});
		}
	});
};
var cityId;
function getSubCity(id) {
	if (id == cityId) {
		return;
	}
	$.ajax({
		url : "getAllSubRegion",
		data : "divisionId=" + id,
		type : "Post",
		dataType : "html",
		success : function(data) {
			cityId = id;
			var citys = eval(data);
			var cityList = $("#citySelect");
			$("#citySelect option").remove();
			var countryList = $("#countrySelect");
			$("#countrySelect option").remove();
			
			$("#streetSelect option").remove();
			
			countryList.append("<option value='-1'>--请选择--</option>");
			cityList.append("<option value='-1'>--请选择--</option>");
			$("#streetSelect").append("<option value='-1'>--请选择--</option>");
			$(citys).each(
					function(key) {
						var opt = $("<option></option>").text(
								citys[key].areaName).val(citys[key].areaId);
						cityList.append(opt);
					});
		}
	});
};
var countryId;
function getSubCountry(id) {
	if (id == countryId) {
		return;
	}
	$.ajax({
		url : "getAllSubRegion",
		data : "divisionId=" + id,
		type : "Post",
		dataType : "html",
		success : function(data) {
			countryId = id;
			var country = eval(data);
			var countryList = $("#countrySelect");
			$("#countrySelect option").remove();
			
			$("#streetSelect option").remove();
			$("#streetSelect").append("<option value='-1'>--请选择--</option>");
			
			countryList.append("<option value='-1'>--请选择--</option>");
			$(country).each(
					function(key) {
						var opt = $("<option></option>").text(
								country[key].areaName).val(
								country[key].areaId);
						countryList.append(opt);
					});
		}
	});
};
var streetId;
function getSubStreet(id) {
	if (id == streetId) {
		return;
	}
	$.ajax({
		url : "getAllSubRegion",
		data : "divisionId=" + id,
		type : "Post",
		dataType : "html",
		success : function(data) {
			countryId = id;
			var country = eval(data);
			var countryList = $("#streetSelect");
			$("#streetSelect option").remove();
			countryList.append("<option value='-1'>--请选择--</option>");
			$(country).each(
					function(key) {
						var opt = $("<option></option>").text(
								country[key].areaName).val(
										country[key].areaId);
						countryList.append(opt);
					});
		}
	});
};

function saveCreate() {
	
	$("#saveCreate").attr("disabled",true);
	
	if (checkNewCreateAddress()) {
		
		var addressName = $("#c_addressName").val();
		var recipientName = $("#c_recipientName").val();
		var mobile = $("#c_mobile").val();
		var telephone = $("#c_telephone").val();
		var Province = $("#ProvinceSelect  option:selected");
		var citys = $("#citySelect  option:selected");
		var country = $("#countrySelect  option:selected");
		var street = $("#streetSelect  option:selected");
		var addressDetail = $("#c_distributionDeatilInfo").val();
        var token = $("#c_token").val();
        var nationPrefix=$("#c_nationPrefix").val();
		$.ajax({
			url : "saveNewCreateAddress",
			data : {
				addressName : addressName,
				recipientName : recipientName,
				mobile : mobile,
				ProvinceId : Province.val(),
				ProvinceName : Province.text(),
				citysId : citys.val(),
				citysName : citys.text(),
				countryId : country.val(),
				countryName : country.text(),
				streetId : street.val(),
				streetName : street.text(),
                nationPrefix : nationPrefix,
				addressDetail : addressDetail,
				telephone : telephone,
                token : token
			},
			type : "Post",
			async : false,
			dataType : "json",
			success : function(data) {
				var ret = eval(data);
				if (ret.success == true) {
                    window.location.reload(true);
                }else{
                	   $("#saveCreate").attr("disabled",false);
                	   var errorMsg = ret.errorMsg;
                    popAlert(errorMsg);
                }
			}
		});
	}else{
		$("#saveCreate").attr("disabled",false);
	}
};

function checkNewCreateAddress() {
	$("#c_nameMsg").text("");
	$("#c_recipientMsg").text("");
	$("#c_mobileMsg").text("");
	$("#c_telephoneMsg").text("");
	$("#c_zipCodeMsg").text("");
	$("#c_peisongMsg").text("");
	$("#c_peisongMsg1").text("");
	$("#c_emailMsg").text("");

	var isCanSubmit = true;
	return isCanSubmit;
};

$(function() {
	voteProgFun([ 15, 23, 69, 42, 62, 42, 71, 36, 52, 68 ], ".pro-bar-in");

});

function deleteAddressById(id) {
	$.ajax({
		url : "queryAddressInfoById",
		data : {
			addressId : id
		},
		type : "Post",
		dataType : "json",
		success : function(data) {
			var ret = eval(data);
			if (ret.preferredStatus == 2) {
				popFunL('.pop-10');
			} else if (ret.verifyStatus == 2) {
				$('#delUsedAddressName').text(ret.addressName);
				$('#delUsedAddressId').val(ret.addressId);

				popFunL('.pop-9');
			} else if (ret.verifyStatus == 1) {
				$('#delNoUsedAddressId').val(ret.addressId);
				$('#delNoUsedAddressName').text(ret.addressName);
				popFunL('.pop-8');
			}
			;
		}
	});
};

function sureDeleteAddress(value) {
	$('#test').hide();
	var id = 0;
	if (value == 1) {
		id = $("#delNoUsedAddressId").val();
	} else if (value == 2) {
		id = $("#delUsedAddressId").val();
	}
	$.ajax({
		url : "deleteAddressInfoById",
		async : false,
		data : {
			addressId : id
		},
		type : "Post",
		async : false,
		dataType : "json",
		success : function(data) {
			if (data == 1) {
				window.location.reload(true);
			} else if (data == 2) {
				popFunL('.pop-10');
			}else if (data == 3) {
				popAlert("收货地址不存在");
			}else{
				popAlert("系统开小差了，请稍后重试");
			}
			;
		}
	});

};

function modifyAddress(id) {
	$.ajax({
        url : "queryAddressInfoById",
        data : {
            addressId : id
        },
        type : "Post",
        dataType : "json",
        success : function(data) {
            var ret = eval(data);
            var str = ret.telephone;
            $('#modifyName' + id + '').val(ret.addressName);
            $('#modifyPerson' + id + '').val(ret.recipientName);
            $('#modifyPersonShow' + id + '').val(ret.recipientName);
            $('#modifyMobileShow' + id + '').val(ret.mobile);
            $('#modifyMobile' + id + '').val(ret.mobile);
            $('#modifyTelephone' + id + '').val(str);
            $('#modifyPrepStatus' + id + '').val(ret.preferredStatus);
            var provinceId = ret.distributionProvinceId;
            var cityId = ret.distributionCityId;
            var countryId = ret.distributionCountyId;
            var streetId = ret.distributionStreetId;
            var nationPrefix = ret.nationPrefix;
            if (nationPrefix == null || nationPrefix == undefined || nationPrefix == '') {
                nationPrefix="86";
            }
            $('#modifyNationPrefixShow' + id + '').val(nationPrefix);

            $.ajax({
                url : "getAllProvince",
                type : "Post",
                dataType : "html",
                success : function(data) {
                    var Province = eval(data);
                    var tempList = $("#MProvinceSelect" + id
                            + "");
                    $("#MProvinceSelect" + id + " option")
                            .remove();
                    tempList
                            .append("<option value='-1'>--请选择--</option>");
                    $(Province)
                            .each(
                                    function(key) {
                                        var opt = "";
                                        if (Province[key].areaId == provinceId) {
                                            opt = $(
                                                    "<option selected ='selected'></option>")
                                                    .text(
                                                            Province[key].areaName)
                                                    .val(
                                                            Province[key].areaId);
                                        } else {
                                            opt = $(
                                                    "<option></option>")
                                                    .text(
                                                            Province[key].areaName)
                                                    .val(
                                                            Province[key].areaId);
                                        }
                                        tempList.append(opt);
                                    });
                }
            });

            $.ajax({
                url : "getAllSubRegion",
                data : "divisionId=" + provinceId,
                type : "Post",
                dataType : "html",
                success : function(data) {
                    var citys = eval(data);
                    var cityList = $("#McitySelect" + id + "");
                    $("#McitySelect option").remove();
                    cityList
                            .append("<option value='-1'>--请选择--</option>");
                    $(citys)
                            .each(
                                    function(key) {
                                        var opt = "";
                                        if (citys[key].areaId == cityId) {
                                            opt = $(
                                                    "<option selected ='selected'></option>")
                                                    .text(
                                                            citys[key].areaName)
                                                    .val(
                                                            citys[key].areaId);
                                        } else {
                                            opt = $(
                                                    "<option></option>")
                                                    .text(
                                                            citys[key].areaName)
                                                    .val(
                                                            citys[key].areaId);
                                        }
                                        cityList.append(opt);
                                    });
                }
            });
            $.ajax({
                url : "getAllSubRegion",
                data : "divisionId=" + cityId,
                type : "Post",
                dataType : "html",
                success : function(data) {
                    var citys = eval(data);
                    var cityList = $("#McountrySelect" + id
                            + "");
                    $("#McountrySelect" + id + " option")
                            .remove();
                    cityList
                            .append("<option value='-1'>--请选择--</option>");
                    $(citys)
                            .each(
                                    function(key) {
                                        var opt = "";
                                        if (citys[key].areaId == countryId) {
                                            opt = $(
                                                    "<option selected ='selected'></option>")
                                                    .text(
                                                            citys[key].areaName)
                                                    .val(
                                                            citys[key].areaId);
                                        } else {
                                            opt = $(
                                                    "<option></option>")
                                                    .text(
                                                            citys[key].areaName)
                                                    .val(
                                                            citys[key].areaId);
                                        }
                                        cityList.append(opt);
                                    });
                }
            });
            $.ajax({
                url : "getAllSubRegion",
                data : "divisionId=" + countryId,
                type : "Post",
                dataType : "html",
                success : function(data) {
                    var citys = eval(data);
                    var cityList = $("#MstreetSelect" + id
                            + "");
                    $("#MstreetSelect" + id + " option")
                    .remove();
                    cityList
                    .append("<option value='-1'>--请选择--</option>");
                    $(citys)
                    .each(
                            function(key) {
                                var opt = "";
                                if (citys[key].areaId == streetId) {
                                    opt = $(
                                            "<option selected ='selected'></option>")
                                            .text(
                                                    citys[key].areaName)
                                                    .val(
                                                            citys[key].areaId);
                                } else {
                                    opt = $(
                                            "<option></option>")
                                            .text(
                                                    citys[key].areaName)
                                                    .val(
                                                            citys[key].areaId);
                                }
                                cityList.append(opt);
                            });
                }
            });
            $('#modifyAddressDetail' + id + '').val(ret.distributionDeatilInfo);
        }
    });

	$('#modify' + id + '').show();
};
function cancleModify(id) {
	$('#m_nameMsg' + id + '').text("");
	$('#m_recipientMsg' + id + '').text("");
	$('#m_mobileMsg' + id + '').text("");
	$('#m_telephoneMsg' + id + '').text("");
	$('#m_peisongMsg' + id + '').text("");
	$('#m_peisongMsg1' + id + '').text("");
	$('#m_zipCodeMsg' + id + '').text("");
	// $('#m_idCardMsg' + id + '').text("");
	$('#m_emailMsg' + id + '').text("");
	$('#modify' + id + '').hide();
};

function getModifyString(id, isfull) {
	var modifyHtml = "";
	modifyHtml += "	  <h2 class='f14 c1 mt10 ml10'><strong>修改地址</strong></h2>";
	if (isfull) {
		modifyHtml += "<p class='m_add_p mt10 pt10 m_line c4'>地址信息不完善，请根据提示补全必要信息。 <a href='http://help.damai.cn/damai/contents/289/5969.html' target='_blank' class='m_c3'>为什么？</a>";
	}
	modifyHtml += "		<dl>";
	modifyHtml += "			<dt class='w80 tr fl'><span class='c4'>*</span>收&nbsp;件&nbsp;人：</dt>";
	modifyHtml += "			<dd class='fl'>" +
		"<input type='text' id='modifyPersonShow"+ id+ "' class='input-text' name=''>" +
		"<span id='m_recipientMsg"+ id+ "' style='color:red'></span>" +
		"<input type='hidden' id='modifyPerson"+ id + "' class='input-text' name=''>" +
		"</dd>";
	modifyHtml += "		</dl>";
	modifyHtml += "		<dl>";
	modifyHtml += "			<dt class='w80 tr fl'><span class='c4'>*</span>手机：</dt>";
	modifyHtml += "			<dd class='fl'>" +
		"<input type='text' id='modifyMobileShow"+ id+ "' class='input-text' name=''>" +
		"<select class='sel-4' id='modifyNationPrefixShow"+ id+ "' >" +
        "  <option value='86'>中国大陆 +86</option>" +
        "  <option value='852'>中国香港 +852</option>" +
        "  <option value='853'>中国澳门 +853</option>" +
        "  <option value='886'>中国台湾 +886</option>" +
        "</select>"+
		"<span id='m_mobileMsg"+ id+ "' style='color:red'>" +
		"</span><input type='hidden' id='modifyMobile"+ id + "' class='input-text' name=''>" +
		"</dd>";
	modifyHtml += "		</dl> ";
	modifyHtml += "		<dl>";
	modifyHtml += "			<dt><span class='c4'>*</span>配送地址：</dt>";
	modifyHtml += "			<dd>";
	modifyHtml += "	       	<p><select class='sel-4' id='MProvinceSelect"
			+ id
			+ "' name='' onchange='getMSubCity(this.value,"
			+ id
			+ ");'><option></option></select>&nbsp;省&nbsp;<select class='sel-4'  id='McitySelect"
			+ id
			+ "' onchange='getMSubCountry(this.value,"
			+ id
			+ ");' name=''></select>&nbsp;市&nbsp;<select class='sel-4'  id='McountrySelect"
			+ id
			+ "' name='' onchange='getMSubStreet(this.value,"+id+");'><option></option></select>&nbsp;区&nbsp;" 
			+ " <select class='sel-4'  id='MstreetSelect"+ id+"' name=''><option></option></select>&nbsp;街道&nbsp;"
			+ " <br> <span id='m_peisongMsg"
			+ id + "' style='color:red'></span></p>";
	modifyHtml += "	           <p class='mt15'><input type='text' id='modifyAddressDetail"
			+ id
			+ "'  name='' class='input-text-1'><span id='m_peisongMsg1"
			+ id + "' style='color:red'></span></p>";
	modifyHtml += "			</dd>";
	modifyHtml += "		</dl>";
//	modifyHtml += "		<dl>";
//	modifyHtml += "			<dt>邮政编码：</dt>";
//	modifyHtml += "		<dd><input type='text' id='modifyZop" + id
//			+ "' class='input-text-2' name='' value=''><span id='m_zipCodeMsg"
//			+ id + "' style='color:red'></span></dd>";
//	modifyHtml += "		</dl>";
//	modifyHtml += "		<dl>";
	/*
	 * modifyHtml += " <dt>身份证号码：</dt>"; modifyHtml += " <dd><input
	 * type='text' id='modifyiCardShow" + id + "' class='input-text-2' name=''
	 * value=''><span id='m_idCardMsg" + id + "' style='color:red'></span><input
	 * type='hidden' id='modifyiCard" + id + "' class='input-text-2' name=''
	 * value=''></dd>"; modifyHtml += " </dl>"; modifyHtml += " <dl>";
	 */
//	modifyHtml += "			<dt>邮箱：</dt>";
//	modifyHtml += "		<dd><input type='text' id='modifyemailShow" + id
//			+ "' class='input-text-2' name='' value=''><span id='m_emailMsg"
//			+ id
//			+ "' style='color:red'></span><input type='hidden' id='modifyemail"
//			+ id + "' class='input-text-2' name='' value=''></dd>";
//	modifyHtml += "		</dl>";
	modifyHtml += "		<p class='per-all-t clear'><button id='saveModifyButton"+id+"' onclick='saveModifyAddress("
			+ id
			+ ")' class='btn05 fl'>保存地址</button><button class='btn04 fl' onclick='cancleModify("
			+ id + ");' style='margin:3px 0 0 20px;'>取消</button></p> ";
	return modifyHtml;
};

function getValidateStr(id) {
	var str = "";
	str += "	<h2 class='f14 c1 mt10 ml10'><strong>验证地址</strong></h2>";
	str += "	<dl class='m_add_dl'>";
	str += "	<dd>安全中心绑定手机：13811****00 <a href='#' class='pd_send_code ml30 m_c5'>获取验证码</a></dd>";
	str += "	<dd>验证码已发送，10分钟内有效。如未收到，1分30秒后可重新获取</dd>";
	str += "	<dd>请输入短信验证码：<input id='MesgCode" + id
			+ "' name='' type='text'/></dd>";
	str += "	<dd><input name='' id='Checkprefer"
			+ id
			+ "' type='checkbox' value='' class='vm mr5'/>将此地址设为“首选地址”。<a href='#' class='m_c3 ml10'>了解首先地址</a></dd>";
	str += "	</dl>";
	str += "<label class='clear pb10'><a href='#' class='pd_send_code ml30 m_c5 mb20 fl'>验证地址</a><a href='#' class='pd_send_code ml10 m_c5 mb20 fl'>取消</a><span class='m_c0 fl mb20'><span class='c4'>*&nbsp;</span>验证后使用优惠券，电子钱包等支付方式更加便捷！</span><a href='#' class='m_c3 ml10'>了解更多</a></label>";
};
function getMSubCity(parentid, id) {
	$.ajax({
		url : "getAllSubRegion",
		data : "divisionId=" + parentid,
		type : "Post",
		dataType : "html",
		success : function(data) {
			var citys = eval(data);
			var cityList = $("#McitySelect" + id + "");
			$("#McitySelect" + id + " option").remove();
			cityList.append("<option value='-1'>--请选择--</option>");
			var countryList = $("#McountrySelect" + id + "");
			$("#McountrySelect" + id + " option").remove();
			countryList.append("<option value='-1'>--请选择--</option>");
			$(citys).each(
					function(key) {
						var opt = $("<option></option>").text(
								citys[key].areaName).val(citys[key].areaId);
						cityList.append(opt);
					});
		},
		error : function(data) {
			alert("Error");
		}
	});
};
function getMSubCountry(parentid, id) {
	$.ajax({
		url : "getAllSubRegion",
		data : "divisionId=" + parentid,
		type : "Post",
		dataType : "html",
		success : function(data) {
			var country = eval(data);
			var countryList = $("#McountrySelect" + id + "");
			$("#McountrySelect" + id + " option").remove();
			countryList.append("<option value='-1'>--请选择--</option>");
			$(country).each(
					function(key) {
						var opt = $("<option></option>").text(
								country[key].areaName).val(
								country[key].areaId);
						countryList.append(opt);
					});
		}
	});
};
function getMSubStreet(parentid, id) {
	$.ajax({
		url : "getAllSubRegion",
		data : "divisionId=" + parentid,
		type : "Post",
		dataType : "html",
		success : function(data) {
			var country = eval(data);
			var countryList = $("#MstreetSelect" + id + "");
			$("#MstreetSelect" + id + " option").remove();
			countryList.append("<option value='-1'>--请选择--</option>");
			$(country).each(
					function(key) {
						var opt = $("<option></option>").text(
								country[key].areaName).val(
								country[key].areaId);
						countryList.append(opt);
					});
		}
	});
};

function saveModifyAddress(id) {
	$("#saveModifyButton"+id).attr("disabled",true);
	if (checkModifyAddress(id)) {
		
		var pStatu = $('#modifyPrepStatus' + id + '').val();
		var addressName = $('#modifyName' + id + '').val();
		var recipientName = $('#modifyPersonShow' + id + '').val();
		if ($('#modifyPerson' + id + '').val() == recipientName) {
			recipientName = $('#modifyPerson' + id + '').val();
		}
		var mobile = $('#modifyMobileShow' + id + '').val();
		if ($('#modifyMobile' + id + '').val() == mobile) {
			mobile = $('#modifyMobile' + id + '').val();
		}
		var telephone = $('#modifyTelephone' + id + '').val();
		var addressDetail = $('#modifyAddressDetail' + id + '').val();
		var Province = $("#MProvinceSelect" + id + "  option:selected");
		var citys = $("#McitySelect" + id + "  option:selected");
		var country = $("#McountrySelect" + id + "  option:selected");
		var street = $("#MstreetSelect" + id + "  option:selected");
		var nationPrefix = $('#modifyNationPrefixShow' + id + '').val();

		$.ajax({
			url : "modifyAddress",
			data : {
				addressId : id,
				addressName : addressName,
				recipientName : recipientName,
				mobile : mobile,
				ProvinceId : Province.val(),
				ProvinceName : Province.text(),
				citysId : citys.val(),
				citysName : citys.text(),
				countryId : country.val(),
				countryName : country.text(),
				streetId : street.val(),
				streetName : street.text(),
                nationPrefix : nationPrefix,
				addressDetail : addressDetail,
				newStatus : pStatu,
				telephone : telephone
			},
			type : "Post",
			dataType : "json",
			success : function(data) {
				var ret = eval(data);
				if (ret.success == true) {
                    window.location.reload(true);
                }else{
                	    $("#saveModifyButton"+id).attr("disabled",false);
                	    var errorMsg = ret.errorMsg;
                    popAlert(errorMsg);
                }
			}
		});
	}else{
		$("#saveModifyButton"+id).attr("disabled",false);
	}
};

function checkModifyAddress(id) {
	$('#m_nameMsg' + id + '').text("");
	$('#m_recipientMsg' + id + '').text("");
	$('#m_mobileMsg' + id + '').text("");
	$('#m_telephoneMsg' + id + '').text("");
	$('#m_peisongMsg' + id + '').text("");
	$('#m_peisongMsg1' + id + '').text("");
	$('#m_zipCodeMsg' + id + '').text("");
	$('#m_emailMsg' + id + '').text("");
	var isCanSubmit = true;
	return isCanSubmit;
};

/*--------------------------------------------设置首先地址部分js-------------------------------------------------*/
function addressAsPreferred(id, name) {
	$('#preferredAddressShow').text(name);
	$('#preferredAddressId').val(id);
	popFunL('.pop-6');

};
function SetupPreferred() {
	var addressId = $('#preferredAddressId').val();
	$.ajax({
		url : "modifyAddressPreferre",
		async : false,
		data : {
			addressId : addressId
		},
		type : "Post",
		dataType : "html",
		success : function(data) {
			if(data==1){
                window.location.reload(true);
            }else if(data==-2){
                popAlert("无效地址不能设置为默认地址");
            }else {
                popAlert("系统开小差了，请稍后重试");
            }
		}
	});

};

function hideMobile(mobile) {
	return mobile.substring(0, 3) + "****" + mobile.substring(7, 11);
}

function hideIdcard(idCard) {
	if (idCard == "" || idCard == null) {
		return "";
	}
	return idCard.substring(0, 8) + "********" + idCard.substring(16, 18);
}

function hideName(name) {
	if (name == "" || name == null) {
		return "";
	} else {
		return "**" + name.substring(name.length - 1, name.length);
	}
}

function hideEmail(email) {
	if (email == "" || email == null) {
		return "";
	} else {
		var str = new Array();
		str = email.split("@");
		var size = str[0].length;
		if (size < 3) {
			return email;
		} else {
			return str[0].substring(0, 1) + "****"
					+ str[0].substring(size - 1, size) + "@" + str[1];
		}
	}
}